package cn.yougou.controller;

import cn.yougou.common.DtoUtil;

import cn.yougou.common.VoDataUtil;
import cn.yougou.dto.Dto;
import cn.yougou.dto.Query;
import cn.yougou.pojo.YgUser;
import cn.yougou.pojo.YgUserBasicsinfo;
import cn.yougou.service.user.YgUserBasicsinfoService;
import cn.yougou.service.user.YgUserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: DELL
 * @date: 2020/12/6 10:20
 * @description:
 */
@RestController
@RequestMapping("/ygAdminUser")
public class YgAdminUserController {
    @Reference
    private YgUserService ygUserService;

    @Reference
    private YgUserBasicsinfoService ygUserBasicsinfoService;

    @Autowired
    private VoDataUtil voDataUtil;
    @GetMapping("/selUserList")
    public Dto selUserList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                           @RequestParam(value = "limit",defaultValue = "10") Integer limit,
                           Integer userId,
                           String username,
                           String mobile,
                           String sort,
                           String order,
                           Integer infoCount) {////////
        //拼接 query 对象
        Query query = new Query(((page-1)*limit),limit,userId,username,mobile,sort,order,infoCount);
       // System.out.println(query);
        //查询 userInfo count
        query.setInfoCount(ygUserService.selCount(query));
        //判断是否有数据
        if(query.getInfoCount() >0){
            //根据 进行主表的分页查询
            List<YgUser> userInfoList = ygUserService.conditionSelAll(query);
            //根据id将附表的信息查出                                   // 根据主表数据遍历出 所有id
            List<YgUserBasicsinfo> UserBasicsinfoList = ygUserBasicsinfoService.selAllById(voDataUtil.returnUserIdList(userInfoList));
            //返回                                                    //拼接vo对象
            return DtoUtil.returnDataListSuccess(voDataUtil.returnYgUserInfoVo(userInfoList,UserBasicsinfoList),query);
        }else {
            return DtoUtil.returnSuccess("没有想要的数据");
        }
    }
/**
 * 修改 用户信息和用户基本信息
 * */
    @GetMapping("userUpdata")
    public Dto userUpdata(Integer id,//id
                          String userid,//用户名
                          String phone,//手机号
                          Integer sex,//性别
                          Integer level,//等级
                          Integer state){//状态
            int result = ygUserService.update(new YgUser(id,userid,phone)) +
                  ygUserBasicsinfoService.insert(new YgUserBasicsinfo(id,sex,level,state));

            if(result >= 2){
                return DtoUtil.returnSuccess("200");
            }else {
                return DtoUtil.returnSuccess("4001");
            }

    }
    /**
     * 为用户充值
     * */
    @GetMapping("chargeMoney")
    public Dto chargeMoney(Integer id,Double money){
        YgUser ygUser = new YgUser(id,money);
        double userMoney = ygUserService.queryById(ygUser.getId()).getMoney();
      //  System.out.println(userMoney);
        if(userMoney + money >9999999){
            return DtoUtil.returnSuccess("4001");
        }
        int result =  ygUserService.chargeMoney(ygUser);
        if (result == 1) {
            return DtoUtil.returnSuccess("200");
        }else {
            return DtoUtil.returnSuccess("4001");
        }
    }
}
